package com.atguigu.gmall.order.config;

import com.atguigu.gmall.task.config.MqConst;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 李旭
 * @create: 2021-07-26 10:08
 *
 * 延迟消息  插件
 **/
@Configuration
public class DelayedMessageConfig {


    //交换机（延迟）
    @Bean
    public Exchange delayExchange(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type", "direct");
        return new CustomExchange(MqConst.EXCHANGE_DIRECT_ORDER_CANCEL,"x-delayed-message"
        ,true,false,arguments);
    }
    //普通队列
    @Bean
    public Queue delayQueue(){
        return QueueBuilder.durable(MqConst.QUEUE_ORDER_CANCEL).build();
    }
    //绑定
    @Bean
    public Binding delayQueueToDelayExchange(@Qualifier(value = "delayQueue") Queue delayQueue,
                                             @Qualifier(value = "delayExchange") Exchange delayExchange){
        return BindingBuilder.bind(delayQueue).to(delayExchange).with(MqConst.ROUTING_ORDER_CANCEL).noargs();
    }
}
